package end.handler.timer;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import end.ser.InitialDataSER;

/**
 * 
 * TimerTaskInitialData 上午10:13:28 Copyright HEXUNz Inc. All right reserved.
 * Email : alldios@outlook.com
 * 
 */
public class TimerTaskForInitialPermissonData extends TimerTask {
	private ServletContext context = null;
	private InitialDataSER initialDataSER = null;
	private static String sqlStr = "";
	static {
		try {
			String rootPath = TimerTaskForInitialPermissonData.class.getResource("/")
					.getPath();
			String SQLMainPath = rootPath + "end/sql/initialPermission.sql";
			InputStreamReader read = new InputStreamReader(new FileInputStream(
					new File(SQLMainPath)), "utf-8");// 考虑到编码格式
			String readLine = null;
			BufferedReader bufferedReader = new BufferedReader(read);
			while ((readLine = bufferedReader.readLine()) != null) {
				if (!readLine.startsWith("/*") || readLine.length() == 0) {
					sqlStr = sqlStr + readLine;
				}
			}
			read.close();
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public TimerTaskForInitialPermissonData(ServletContext context,
			InitialDataSER initialDataSER) {
		this.context = context;
		this.initialDataSER = initialDataSER;
	}

	@Override
	public void run() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		context.log(format.format(new Date()) + " + 执行指定定時任务开始");
		initialDataSER.initialMainData(sqlStr);
		context.log(format.format(new Date()) + " + 指定定時任务执行结束");
	}
}